
//
//  SearchEquipmentVC.m
//  RTUApp
//
//  Created by 陈德军 on 2017/5/18.
//  Copyright © 2017年 陈德军. All rights reserved.
//

//搜索设备

#import "SearchEquipmentVC.h"
#import "SearchEquipmentModel.h"
#import "SearchEquipmentTableViewCell.h"


@interface SearchEquipmentVC ()<UITableViewDataSource, UITableViewDelegate,SearchEquipmentTableViewCellDelegate>{
    NSInteger k;
}

/**
 *  tableView
 */
@property (nonatomic,strong) UITableView *tabView;
/**
 *  数据数组
 */
@property (nonatomic,strong) NSMutableArray<SearchEquipmentModel *> *dataArr;
/**
 *  数组
 */
@property (nonatomic,strong) NSMutableArray<CBPeripheral *> *dataArrL;
/**
 *  设备
 */
@property (nonatomic,strong) CBPeripheral *peripheral;
@end

@implementation SearchEquipmentVC

- (void)viewDidLoad {
    [super viewDidLoad];
    self.view.backgroundColor = [UIColor whiteColor];
    self.title = @"搜索设备";
    self.dataArr = [NSMutableArray new];
    self.dataArrL = [NSMutableArray new];
    [kHMBlueToothManager beginScanCBPeripheral:^(NSArray *peripheralArr) {
        for (CBPeripheral *periheral in peripheralArr) {
            if (![self.dataArrL containsObject:periheral]) {
                SearchEquipmentModel *model = [SearchEquipmentModel new];
                model.num = [NSString stringWithFormat:@"%ld",(long)k++];
                model.name =periheral.name;
                model.macID = [NSString stringWithFormat:@"%@", periheral.identifier];
                if (periheral.state == 0) {
                    model.butTitle = @"连接";
                }else{
                    model.butTitle = @"断开";
                }
                
                [self.dataArr addObject:model];
                [self.dataArrL addObject:periheral];
            }
        }
        [self.tabView reloadData];
    }];
    [self addView];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

- (void)addView{
    self.tabView = [[UITableView alloc ] init];
    self.tabView.tableFooterView = [[UIView alloc] initWithFrame:CGRectZero];
    self.tabView.size = CGSizeMake(kScreenWidth, kScreenHeight-64);
    self.tabView.top = 0;
    self.tabView.left = 0;
    self.tabView.delegate = self;
    self.tabView.dataSource = self;
    [self.tabView registerClass:[SearchEquipmentTableViewCell class] forCellReuseIdentifier:@"cell"];
    [self.view addSubview:self.tabView];
    
}

#pragma mark - UITableViewDelegeta 代理分组
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return self.dataArr.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    SearchEquipmentTableViewCell *cell = [SearchEquipmentTableViewCell cellWithtableView:tableView];
    cell.delegate = self;
    cell.selectionStyle = UITableViewCellSelectionStyleNone;
    [cell setModel:self.dataArr[indexPath.row]];
    return cell;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    [tableView deselectRowAtIndexPath:indexPath animated:YES];// 取消选中
}

#pragma mark - SearchEquipmentTableViewCellDelegate 代理分组
-(void)didClickButton:(UIButton *)button
{
    SearchEquipmentTableViewCell *cell = (SearchEquipmentTableViewCell *)button.superview.superview;
    NSIndexPath *indexPath = [self.tabView indexPathForCell:cell];
    NSLog(@"%@",indexPath);
    
    self.peripheral = self.dataArrL[indexPath.row];
    
    if ([self.dataArr[indexPath.row].butTitle isEqualToString:@"连接"]) {
        [kHMBlueToothManager connectPeripheral:self.peripheral Completion:^(NSError *error) {
            if (error == nil) {
                NSLog(@"连接成功");
                [self.navigationController popViewControllerAnimated:YES];
                [[NSUserDefaults standardUserDefaults] setObject:@"连接成功" forKey:@"ConnectionStatus"];
                [PopupView showMessageStr:@"连接成功"];
            }else{
                NSLog(@"连接失败");
                [[NSUserDefaults standardUserDefaults] setObject:@"连接失败" forKey:@"ConnectionStatus"];
                [PopupView showMessageStr:@"连接失败"];
            }
        }];
    }else if ([self.dataArr[indexPath.row].butTitle isEqualToString:@"断开"]){
        [kHMBlueToothManager disconnectPeripheral:self.peripheral Completion:^(NSError *error) {
            
            SearchEquipmentModel *model = [SearchEquipmentModel new];
            model.num = [NSString stringWithFormat:@"%ld",(long)k++];
            model.name =self.peripheral.name;
            model.macID = [NSString stringWithFormat:@"%@", self.peripheral.identifier];
            model.butTitle = @"连接";
            [self.dataArr replaceObjectAtIndex:indexPath.row withObject:model];
            [self.tabView reloadData];
            [PopupView showMessageStr:@"断开连接"];
            self.navigationController.navigationBar.barTintColor = KK_MailColor;
            [[NSUserDefaults standardUserDefaults] setObject:@"连接失败" forKey:@"ConnectionStatus"];
        }];
    }
    
    
}

#pragma mark - 懒加载

@end
